﻿using FE.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FineUIPro.EmptyProjectNet40
{
    public partial class departmentmanage : PageBase
    {
        FE.BLL.departments deparmentbll = new FE.BLL.departments();
        FE.BLL.users userbll = new FE.BLL.users();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["login"] == null || Session["login"].ToString() != "sucess")
            {
                PageContext.RegisterStartupScript("alertAndRedirect('请登录', 'login.aspx?url=departmentmanage.aspx');");

                return;
            }
            if (!IsPostBack)
            {
                //btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项！");
                //btnDelete.ConfirmText = String.Format("你确定要删除选中的&nbsp;<b><script>{0}</script></b>&nbsp;行数据吗？", Grid1.GetSelectedCountReference());
                var dptlist = deparmentbll.GetAllList();
             
                // 绑定表格
                BindGrid();

                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
            }
        }

        private void BindGrid()
        {
            var dptlist = deparmentbll.GetAllList();
            Grid1.DataSource = dptlist;
            Grid1.DataBind();
        }

        // 删除数据
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowID != "")
            {

                DeleteRowByID(Grid1.DataKeys[Grid1.SelectedRowIndex][0].ToString());
                BindGrid();
                //ShowNotify("删除数据成功!（表格数据已重新绑定）");
            }
            else
            {
                Alert.Show("请选择要删除的项！");
            }

        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {

        }


        // 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Alert.Show(hfFormID.Text);
            if (hfFormID.Text == "")
            {

                var model = new departments();
                model.id =int.Parse(txtNum.Text);
                model.dptName = txtName.Text;
                deparmentbll.Add(model);
                BindGrid();

                PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, hfFormID.Text) + Window1.GetHideReference());
            }
            else
            {
                // Alert.Show("编辑");

                var model = deparmentbll.GetModel(int.Parse(hfFormID.Text));
                model.dptName = txtName.Text;
                deparmentbll.Update(model);

                BindGrid();
                PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, hfFormID.Text) + Window1.GetHideReference());
            }
        }





        #region 


        // 根据行ID来删除行数据
        private void DeleteRowByID(string id)
        {
            //var dpt = deparmentbll.GetModel(int.Parse(id));
            var list = userbll.GetRecordCount(" DepartmentId="+id);
            if (list > 0)
            {
                FineUIPro.Alert.Show("通讯录已存在此部门信息，请编辑通讯录信息后再尝试删除！");
                return;
            }
            else
            {
                deparmentbll.Delete(int.Parse(id));
                BindGrid();
            }
            //db.users.Remove(user);
            //db.SaveChanges();
            //userbll.Delete(id);
        }



        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

        }




        #endregion

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var rowid = Grid1.SelectedRowID;
            if (string.IsNullOrEmpty(rowid))
            {
                FineUIPro.Alert.Show("请选择要编辑的项！");
                return;
            }
            else
            {
                var department = deparmentbll.GetModel(int.Parse(rowid));
                //var user = db.users.Where(p => p.id == rowid).FirstOrDefault();

                if (department == null)
                {
                    Alert.Show("数据异常！请稍后再试。");
                    return;
                }
                else
                {
                    this.Window1.Hidden = false;
                    this.Window1.Title = "编辑数据";
                    hfFormID.Text = department.id.ToString();
                    txtName.Text = department.dptName;
                    txtNum.Text = department.id.ToString();
                }
            }


        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
    }
}